home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / tools / interp.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  269 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    interp -
  19.  *        Interpolate between colors.    
  20.  *
  21.  *                Henry Moreton - 1985
  22.  */
  23. #include "gl.h"
  24. #include "math.h"
  25. #include "stdio.h"
  26. #include "device.h"
  27.  
  28. #define MINX 0.0
  29. #define MINY 0.0
  30. #define MAXX 20.0
  31. #define MAXY 30.0
  32.  
  33. #define MIN(x,y) ((x) > (y) ? (y) : (x))
  34.  
  35. int startcolor;
  36. int endcolor;
  37. char pickingstart = TRUE;
  38. float startr,startg,startb;
  39. float endr,endg,endb;
  40.  
  41. char *colormodels[] =     {
  42.     "rgb", "cmy", "hsv", "hls"
  43. };
  44.  
  45. main(argc, argv)
  46. int argc;
  47. char **argv;
  48. {
  49.     short    dev, val;
  50.     int        scrx, scry;
  51.     short    mousex, mousey;
  52.     int     titles;
  53.     int        sel, menu;
  54.  
  55.     exit(0);
  56.     startcolor = greybase();
  57.     endcolor = startcolor+127;
  58.     titles = 0;
  59.     if (argc > 1) {
  60.     setcolorsys(atoi(argv[1]));
  61.         titles = 1;
  62.     }
  63.     keepaspect(3,2);
  64.     glcompat(GLC_SOFTATTACH, TRUE);
  65.     winopen("interp");
  66.     ortho2(MINX, MAXX, MINY, MAXY);
  67.     qdevice(MIDDLEMOUSE);
  68.     qdevice(MENUBUTTON);
  69.     qdevice(LEFTMOUSE);
  70.     tie(LEFTMOUSE, MOUSEX, MOUSEY);
  71.     menu = defpup("colorsys %t|rgb|cmy|hsv|hls");
  72.     if (titles)
  73.     wintitle(colormodels[atoi(argv[1])-1]);
  74.     reshapeviewport();
  75.     drawit();
  76.     while (1)  {
  77.     switch (dev = qread(&val)) {
  78.         case REDRAW :
  79.         reshapeviewport();
  80.         drawit();
  81.         break;
  82.         case LEFTMOUSE :
  83.         dev = qread(&mousex);
  84.         dev = qread(&mousey);
  85.         if (val) {
  86.             if (pickingstart)
  87.             startcolor = getapixel(mousex,mousey);
  88.             else
  89.             endcolor = getapixel(mousex,mousey);
  90.             pickingstart = !pickingstart ;
  91.             drawit();
  92.         }
  93.         break;
  94.         case MIDDLEMOUSE :
  95.         if (val) 
  96.             interpolate();
  97.             break;
  98.         case MENUBUTTON :
  99.         sel = dopup(menu);
  100.         if ((sel > 0) && (sel <= 4)) {
  101.             setcolorsys(sel);
  102.             if (titles)
  103.             wintitle(colormodels[sel-1]);
  104.             drawit();
  105.             interpolate();
  106.         }
  107.         break;
  108.     }
  109.     }
  110. }
  111.  
  112. drawit()
  113. {
  114.     grey(0.9);
  115.     clear();
  116.     setupstartcolor();
  117.     setupendcolor();
  118.     drawramp();
  119. }
  120.  
  121. setupstartcolor()
  122. {
  123.     unsigned short sr,sg,sb;
  124.     float fr,fg,fb;
  125.  
  126.     color(startcolor);
  127.     rectfi((int)(1+MINX), 
  128.        (int)(MINY+1+(MAXY-MINY)*0.25), 
  129.        (int)(MINX-1+(MAXX-MINX)/2.0), 
  130.        (int)(MAXY-1));
  131.  
  132.     if (pickingstart) {
  133.     linewidth(3);
  134.     rgb(1.0,0.0,0.0);
  135.     recti((int)(1+MINX), (int)(MINY+1+(MAXY-MINY)*0.25), 
  136.        (int)(MINX-1+(MAXX-MINX)/2.0), (int)(MAXY-1));
  137.         linewidth(1);
  138.     }
  139.     grey(0.0);
  140.     recti((int)(1+MINX), (int)(MINY+1+(MAXY-MINY)*0.25), 
  141.        (int)(MINX-1+(MAXX-MINX)/2.0), (int)(MAXY-1));
  142.  
  143.     modgetmcolor(startcolor,&startr,&startg,&startb);
  144. }
  145.  
  146. setupendcolor()
  147. {
  148.     color(endcolor);
  149.     rectfi((int)(1+MINX+(MAXX-MINX)/2.0), 
  150.        (int)(MINY+1+(MAXY-MINY)*0.25), 
  151.        (int)(MAXX-1.0), 
  152.        (int)(MAXY-1));
  153.     if (!pickingstart) {
  154.     linewidth(3);
  155.     rgb(1.0,0.0,0.0);
  156.     recti((int)(1+MINX+(MAXX-MINX)/2.0), (int)(MINY+1+(MAXY-MINY)*0.25), 
  157.        (int)(MAXX-1.0), (int)(MAXY-1));
  158.         linewidth(1);
  159.     }
  160.     grey(0.0);
  161.     recti((int)(1+MINX+(MAXX-MINX)/2.0), (int)(MINY+1+(MAXY-MINY)*0.25), 
  162.        (int)(MAXX-1.0), (int)(MAXY-1));
  163.  
  164.     modgetmcolor(endcolor,&endr,&endg,&endb);
  165. }
  166.  
  167. interpolate()
  168. {
  169.     float a, b, c;
  170.     float da, db, dc;
  171.     float red, green, blue;
  172.     unsigned int ired, igreen, iblue;
  173.     int color, delcolor;
  174.  
  175.     modgetmcolor(startcolor,&startr,&startg,&startb);
  176.     modgetmcolor(endcolor,&endr,&endg,&endb);
  177.     delcolor = endcolor-startcolor;
  178.     a = startr; 
  179.     b = startg; 
  180.     c = startb;
  181.     da = endr - a;
  182.     db = endg - b;
  183.     dc = endb - c;
  184.  
  185.     if (delcolor) {
  186.     da /= abs(delcolor);
  187.     db /= abs(delcolor);
  188.     dc /= abs(delcolor);
  189.     }
  190.     if (delcolor < 0)
  191.     for (color = startcolor; color >= endcolor; color--) {
  192.         if(color!=startcolor && color !=endcolor) 
  193.         modmapcolor(color,a,b,c);
  194.         a += da;
  195.         b += db;
  196.         c += dc;
  197.  
  198.     }
  199.     else
  200.     for (color = startcolor; color <= endcolor; color++) {
  201.         if(color!=startcolor && color !=endcolor) 
  202.         modmapcolor(color,a,b,c);
  203.         a += da;
  204.         b += db;
  205.         c += dc;
  206.     }
  207. }
  208.  
  209. drawramp()
  210. {
  211.     unsigned short intens[4];
  212.     float parray[4][2];
  213.  
  214.     intens[0] = startcolor;
  215.     intens[1] = startcolor;
  216.     intens[2] = endcolor;
  217.     intens[3] = endcolor;
  218.  
  219.     parray[0][0] = MINX+1.0; 
  220.     parray[0][1] = MINY+1.0; 
  221.     parray[1][0] = MINX+1.0; 
  222.     parray[1][1] = MINY-1.0+(MAXY-MINY)*0.25;
  223.     parray[2][0] = MAXX-1.0;
  224.     parray[2][1] = MINY-1.0+(MAXY-MINY)*0.25;
  225.     parray[3][0] = MAXX-1.0;
  226.     parray[3][1] = MINY+1.0; 
  227.  
  228.     splf2(4, parray, intens);
  229.     color(BLACK);
  230.     rect(MINX+1.0, MINY+1.0, MAXX-1.0, MINY-1.0+(MAXY-MINY)*0.25);
  231. }
  232.  
  233. float clamp(f)
  234. float f;
  235. {
  236.     if(f<0.0)
  237.     f = 0.0;
  238.     if(f>1.0)
  239.     f = 1.0;
  240.     return f;
  241. }
  242.  
  243. modmapcolor(c,r,g,b)
  244. int c;
  245. float r, g, b;
  246. {
  247.     float fr, fg, fb;
  248.     int ir, ig, ib;
  249.  
  250.     torgb(r,g,b,&fr,&fg,&fb);
  251.     rgb_to_irgb(fr,fg,fb,&ir,&ig,&ib);
  252.     gammapcolor(c,ir,ig,ib);
  253. }
  254.  
  255. modgetmcolor(c,r,g,b)
  256. int c;
  257. float *r, *g, *b;
  258. {
  259.     unsigned short cr, cg, cb;
  260.     float fr, fg, fb;
  261.  
  262.     gamgetmcolor(c,&cr,&cg,&cb);
  263.     irgb_to_rgb(cr,cg,cb,&fr,&fg,&fb);
  264.     fromrgb(fr,fg,fb,r,g,b);
  265.     *r = clamp(*r);
  266.     *g = clamp(*g);
  267.     *b = clamp(*b);
  268. }
  269.